package com.love.slidingWindow;

import java.util.LinkedList;
import java.util.List;

public class YiWei {


    public static void main(String[] args) {
        findAnagrams("abdccab","abc");
    }


    public static List<Integer> findAnagrams(String s, String p) {
        int n = s.length();
        int l = 0,r = 0;
        int[] w = new int[26];
        int[] table = new int[26];
        List<Integer> res = new LinkedList<>();
        for (int i = 0; i < p.length(); i++) {
            char c = p.charAt(i);
            table[c - 'a']++;
        }
        int valid = 0;
        while (r < n){
            char c = s.charAt(r);
            w[c - 'a']++;
            r++;
            if(table[c - 'a'] > 0){
                if(w[c - 'a'] <= table[c - 'a']){
                    valid++;
                }
            }

            // 固定窗口(l 指针移动)
            while (r - l >= p.length()){
                char lc = s.charAt(l);
                if(valid == p.length()){
                    res.add(l);
                }
                l++;
                if(table[lc - 'a'] > 0){
                    if(w[lc - 'a'] <= table[lc - 'a']){
                        valid--;
                    }
                }
                w[lc - 'a']--;
            }
        }
        return res;
    }


}
